<template>
	<view class="popup" v-show="show">
		<view class="bg" @tap="cancelMultiple"></view>
	    <view class="selectMultiple" :animation="animationData">
	    	<view class="multipleBody">
	    		<view class="title">
	    			<view class="close" @tap="cancelMultiple">取消</view>
	    			<view class="name">{{title}}</view>
	    			<view class="confirm" @tap="confirmMultiple">确认</view>
	    		</view>
	    		<view class="list">
					<view class="select-all" :class="{checked:selectAll}" @tap="handleSelectAll">全部</view>
					<!-- <view class="mask mask-top"></view>
					<view class="mask mask-bottom"></view> -->
	    			<scroll-view class="diet-list" scroll-y="true" v-if="list.length>0">
	    				<view v-for="(item, index) in list" :key="index" :class="['item', item.selected ? 'checked' : '']" @tap="onChange(index, item)">
	    					<text>{{item.label}}</text>
							<checkbox :value="item.value" :checked="item.selected" style="transform:scale(0.8)"/>
	    				</view>
	    			</scroll-view>
					<view class="no-data" v-else>{{noData}}</view>
	    		</view>
	    	</view>
	    </view>
	</view>
</template>

<script>
	export default {
		name:"multiple-picker",
		data() {
			return {
				// 选中值
				value: [],
				// 选中列表
				selected: [],
				// 列表数据
				list: [],
				// 出场动画
				animationData: {},
				noData:'没有更多内容了',
				selectAll:false,
			};
		},
		props: {
			// 是否显示
			show: {
				type: Boolean,
				default: false
			},
			// 标题
			title: {
				type: String,
				default: ''
			},
			//数据列表
			columns: {
				type: Array,
				default:()=> []
			},
			// 默认选中
			defaultIndex: { 
				type: Array, 
				default:()=> []
			}
		},
		watch: {
			// 监听是否显示
			show(val) {
				if(val) {
					this.openMultiple();
				}
			},
					
			selected:{
				handler(value){
					if(value.length==this.columns.length){
						this.selectAll=true
					}else{
						this.selectAll=false
					}
				},
				immediate:true,
				deep:true
			},

		},
		methods: {
			// 列点击事件
			onChange(index, item) {
				// 是否已选中
				if(this.value.indexOf(item.value) >= 0) {
					this.list[index].selected = false;
				}
				else {
					this.list[index].selected = true;
				}
				// 筛选已勾选数据
				this.value = [];
				this.selected = [];
				this.list.forEach((col_item, col_index) => {
					if(col_item.selected) {
						this.value.push(col_item.value);
						this.selected.push({
							label: col_item.label,
							value: col_item.value,
						});
					}
				});
				this.$emit("change", {selected: this.selected, value: this.value});
			},
			// 弹出框开启触发事件
			openMultiple() {
				// 初始化列表数据,默认勾选数据
				this.value = this.defaultIndex;
				this.selected=[]
				this.columns.forEach((item, index) => {
					this.$set(item, "selected", false);
					if(this.value.indexOf(item.value) >= 0) {
						item.selected = true;
						this.selected.push({
							label: item.label,
							value: item.value,
						});
					}
				});
				this.list = Object.assign([], this.columns);
				// 弹出动画
				this.openAnimation();
			},
			// 确认
			confirmMultiple() {
				this.$emit("confirm", {selected: this.selected, value: this.value});
			},
			// 关闭/取消
			cancelMultiple() {
				this.$emit("cancel");
			},
			// 展开动画
			openAnimation() {
				var animation = uni.createAnimation()
				animation.translate(0, 300).step({ duration: 0 });
				this.animationData = animation.export();
				this.$nextTick(() => {
					animation.translate(0, 0).step({ duration: 300, timingFunction: 'ease' });
					this.animationData = animation.export()
				})
			},
			
			// 点击全选
			handleSelectAll(){	
				this.list.forEach(item=>{
					if(!this.selectAll){
						item.selected=true
					}else{
						item.selected=false
					}
				})
				this.selectAll=!this.selectAll
				
				this.value=[]
				this.selected=[]
				this.list.forEach((col_item, col_index) => {
					if(col_item.selected) {
						this.value.push(col_item.value);
						this.selected.push({
							label: col_item.label,
							value: col_item.value,
						});
					}
				})
			},
		}
	}
</script>

<style scoped lang="scss">
	/deep/::-webkit-scrollbar {
		display: none;
		width: 0 !important;
		height: 0 !important;
		-webkit-appearance: none;
		background: transparent;
	}
	.popup {
		width: 100%;
		height: 100vh;
		position: fixed;
		z-index: 99999;
		left: 0;
		bottom: 0;
		
		.bg {
			width: 100%;
			height: 100%;
			background-color: rgba(black, .5);
		}
	}
	.selectMultiple {
		width: 100%;
		position: absolute;
		left: 0;
		bottom: 0;
		background-color: white;
		
		.multipleBody {
			width: 100%;
			box-sizing: border-box;
			padding-bottom: 80rpx;
			
			.title {
				font-size: 34rpx;
				display: flex;
				flex-direction: row;
				padding: 30rpx;
				
				.close {
					width: 80rpx;
					opacity: .5;
				}
				.name {
					flex: 1;
					text-align: center;
					overflow: hidden;
					display: -webkit-box;
					-webkit-box-orient:vertical;
					-webkit-line-clamp:1;
				}
				.confirm {
					width: 80rpx;
					text-align: right;
					color: #45B250;
				}
			}
			.list {
				width: 100%;
				padding: 30rpx;
				box-sizing: border-box;
				position: relative;
				
				.mask {
					width: 100%;
					height: 120rpx;
					position: absolute;
					left: 0;
					z-index: 2;
					pointer-events: none;
					
					&.mask-top {
						top: 30rpx;
						background-image: linear-gradient(to bottom, #fff, rgba(#fff, 0));
					}
					&.mask-bottom {
						bottom: 0;
						background-image: linear-gradient(to bottom, rgba(#fff, 0), #fff);
					}
				}
				.select-all{
					padding: 20rpx 40rpx;
				}
				.checked {
					color: #45B250;
				}
				
				.diet-list {
					max-height: 400rpx;
					.item {
						position: relative;
						width: 100%;
						line-height: 40rpx;
						border-bottom: 1px solid rgba($color: #000000, $alpha: .05);
						padding: 20rpx 0;
						font-size: 30rpx;
						box-sizing: border-box;
						display: flex;
						align-items: center;
						justify-content: space-between;
						text {
							overflow: hidden;
							display: -webkit-box;
							-webkit-box-orient:vertical;
							-webkit-line-clamp:1;
							padding: 0 40rpx;
						}
						
						&:last-child {
							// border-bottom: none;
							// margin-bottom: 60rpx;
						}
						&:first-child {
							// margin-top: 60rpx;
						}
					}
				}
				.no-data{
					color:#999;
					font-size: 30rpx;
					text-align: center;
				}
			}
		}
	}
</style>
